package edu.hawaii.myisern.action;

import java.util.Iterator;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.model.MyIsernModel;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.researchers.jaxb.Researcher;
import edu.hawaii.myisern.xml.MyIsernXmlLoader;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;

/**
 * Handles the Main Menu interface of the MyIsern Web Application.
 * 
 * @author Sonwright M. Gomez
 *
 */
public class MainMenuActionBean extends MyIsernActionBean {
  /** The singleton MyIsernModel. */
  private MyIsernModel myIsern = MyIsernModel.getInstance();
  private MyIsernXmlLoader mixl;
  private Researcher researcher = new Researcher();
  private Organization organization = new Organization();
  private Collaboration collaboration = new Collaboration();
  
  /** Private fields needed for researcher fields */
 // @Validate(required = true)
  private String possibleResearcherName;
  private String possibleOrganizationName;
  private String possibleCollaborationName;
  /** An error string, always displayed, but invisible if empty. */
  private String errorMessage = "";
  private String mainMenu = "/mainMenu.jsp";

  /**
   * Returns the error message, which may be empty.
   * 
   * @return The error message. 
   */
  public String getErrorMessage() {
    return this.errorMessage;
  }
  
  /**
   * Set's the error message for the user.
   * 
   * @param errorMessage containing error Message.  
   */
  public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
  }
  
  /**
   * Provides an iterator over the Researcher list to the page. 
   * @return The Researcher iterator.
   */
  public Iterator<Researcher> getResearcherIterator() {
    return myIsern.rIterator();
  }

  /**
   * Set's possible name.
   * 
   * @param name containing name.
   */
  public void setPossibleOrganizationName(String name) {
    this.possibleOrganizationName = name;
  }
  
  /**
   * Set's possible name.
   * 
   * @param name containing name.
   */
  public void setPossibleResearcherName(String name) {
    this.possibleResearcherName = name;
  }
  
  /**
   * Set's possible name.
   * 
   * @param name containing name.
   */
  public void setPossibleCollaborationName(String name) {
    this.possibleCollaborationName = name;
  }

  
  /**
   * Get's the possible name.
   * 
   * @return possibleName The possible name.
   */
  public String getPossibleOrganizationName() {
    return possibleOrganizationName;
  }
  
  /**
   * Get's the possible name.
   * 
   * @return possibleName The possible name.
   */
  public String getPossibleCollaborationName() {
    return possibleCollaborationName;
  }
  
  /**
   * Searches for a Researcher, Organization, or Collaboration by name.
   * 
   * @return Resolution to display print page or the main menu if
   * nothing is found.
   */
  public Resolution display () {
    //NEED TO WORK ON ------------------------------+++++

    return new ForwardResolution("/display.jsp");
  }
  
  /**
   * Get's the possible name.
   * 
   * @return possibleName The possible name.
   */
  public String getPossibleResearcherName() {
    return possibleResearcherName;
  }
  
  /**
   * Loads pre-edited Researcher Textfields.
   * 
   * @return Resolution to redisplay a jsp page.
   */
  public Resolution preEditResearcher() {
    try {
      if (myIsern.getResearcherIds().contains(getPossibleResearcherName().replace(' ', '_'))) {
        this.researcher = this.myIsern.getResearcher(this.possibleResearcherName);
        return new RedirectResolution("/editResearcher.jsp").flash(this);
      }
      else {
        setErrorMessage("There was no match found in the Researcher " +
            "database. Please check spelling for any errors.");
        return new ForwardResolution (this.mainMenu);
      }
    }
    catch (NullPointerException e) {
      setErrorMessage("Researcher Field Blank.");
      return new ForwardResolution(this.mainMenu);
    }
  }
  
  /**
   * Loads pre-edited Organization to textfields.
   * 
   * @return Resoluton to redisplay a jsp page.
   */
  public Resolution preEditOrganization() {
    try {
      if (myIsern.getOrganizationIds().contains
          (getPossibleOrganizationName().replace(' ', '_'))) {
        this.organization = this.myIsern.getOrganization(this.possibleOrganizationName);
        return new RedirectResolution("/editOrganization.jsp").flash(this);
      }
      else {
        setErrorMessage("There was no match found in the Organization " +
            "database. Please check spelling for any errors.");
        return new ForwardResolution(this.mainMenu);
      }
    }
    catch (NullPointerException e) {
      setErrorMessage("Organization field blank");
      return new ForwardResolution(this.mainMenu);
    }
  }
  
  /**
   * Loads pre-edited Collaboration to textfields.
   * 
   * @return Resoluton to redisplay a jsp page.
   */
  public Resolution preEditCollaboration() {
    try {
      if (myIsern.getCollaborationIds().contains
          (getPossibleCollaborationName().replace(' ', '_'))) {
        this.collaboration = this.myIsern.getCollaboration(this.possibleCollaborationName);
        return new RedirectResolution("/editCollaboration.jsp").flash(this);
      }
      else {
        setErrorMessage("There was no match found in the Collaboration " +
            "database. Please check spelling for any errors.");
        return new ForwardResolution (this.mainMenu);
      }
    }
    catch (NullPointerException e) {
      setErrorMessage("Collaboration field blank");
      return new RedirectResolution(this.mainMenu);
    }
  }
  /**
   * A handler that performs the action when the 'login' button is pressed.
   * 
   * @return A Resolution indicating to display the addresearcher.jsp page.
   */
  @DefaultHandler
  public Resolution newResearcher () {
    return new ForwardResolution("/addResearcher.jsp");
  }
  
  /**
   * A handler that performs the action when the 'login' button is pressed.
   * 
   * @return A Resolution indicating to display the addresearcher.jsp page.
   */
  public Resolution newOrganization () {
    return new ForwardResolution("/addOrganization.jsp");
  }
  
  /**
   * A handler that performs the action when the 'login' button is pressed.
   * 
   * @return A Resolution indicating to display the addresearcher.jsp page.
   */
  public Resolution newCollaboration () {
    return new ForwardResolution("/addCollaboration.jsp");
  }
 
  
}
